﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Utils;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Common.Enums;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmCustomerServicePlan : Form
    {
        private DataSet dsCustomerServiceItemDetail;
        private DBEntityCustomerServiceItem _customerserviceitem;
        public frmCustomerServicePlan()
        {
            InitializeComponent();
        }

        public frmCustomerServicePlan(DBEntityCustomerServiceItem customerserviceitem) : this()
        {
            this._customerserviceitem = customerserviceitem;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (gvPlan.RowCount <= 0)
            {
                MsgBox.MsgInfo("请先设置周期。");
                return;
            }
            if (!dsCustomerServiceItemDetail.HasChanges())
            {
                return;
            }
            string error = string.Empty;
            bool bFlag = BussinessProxyManager.Instance.SubmitCustomerServiceItmeReservationTime(dsCustomerServiceItemDetail, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("保存会员服务项目服务计划失败，" + error);
                return;
            }
            else
            {
                MsgBox.MsgInfo("保存成功。");
                InitCustomerServiceItemDetail();
            }
        }

        private void btnReference_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            DataSet dsStand = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryServiceItemDetail(_customerserviceitem.SERVICEITEMID, out dsStand, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取最佳服务计划失败\r\n" + error);
                return;
            }

            foreach (DataRow dr in dsStand.Tables[0].Rows)
            {
                DataRow drNew = dsCustomerServiceItemDetail.Tables[0].NewRow();
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CSEQNUM] = dr[DBEntityServiceItemDetail.FieldName.SEQNUM];
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CDAYS] = dr[DBEntityServiceItemDetail.FieldName.SDAYS];
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CMEMO] = dr[DBEntityServiceItemDetail.FieldName.SMEMO];
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CUSTOMERSERVICEITEMID] = _customerserviceitem.CUSTOMERSERVICEITEMID;
                drNew["ISSTANDARDPLAN"] = 1;
                dsCustomerServiceItemDetail.Tables[0].Rows.Add(drNew);
            }
            if (dsStand.Tables[0].Rows.Count > 0)
            {
                gdPlan.DataSource = null;
                gdPlan.DataSource = dsCustomerServiceItemDetail.Tables[0].DefaultView;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmServiceItemPlanAdd form = new frmServiceItemPlanAdd();
            form.Mode = ControlClassModeEnum.emInsert;
            form.Type = 2;
            if (form.ShowDialog() == DialogResult.OK)
            {
                DBEntityCustomerServiceItemReservationTime detail = form.CustomerServiceItemReservationTime;
                DataRow drNew = dsCustomerServiceItemDetail.Tables[0].NewRow();
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CSEQNUM] = detail.CSEQNUM;
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CDAYS] = detail.CDAYS;
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CMEMO] = detail.CMEMO;
                drNew[DBEntityCustomerServiceItemReservationTime.FieldName.CUSTOMERSERVICEITEMID] = _customerserviceitem.CUSTOMERSERVICEITEMID;
                dsCustomerServiceItemDetail.Tables[0].Rows.Add(drNew);
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int iRowHandler = gvPlan.FocusedRowHandle;
            if (iRowHandler < 0)
            {
                MsgBox.MsgInfo("请选择要修改的记录。");
                return;
            }

            DataRow dr = gvPlan.GetDataRow(iRowHandler);
            DBEntityCustomerServiceItemReservationTime detail = new DBEntityCustomerServiceItemReservationTime();
            detail.CSEQNUM = Convert.ToInt32(dr[DBEntityCustomerServiceItemReservationTime.FieldName.CSEQNUM]);
            detail.CDAYS = Convert.ToInt32(dr[DBEntityCustomerServiceItemReservationTime.FieldName.CDAYS]);
            object objMemo = dr[DBEntityCustomerServiceItemReservationTime.FieldName.CMEMO];
            if (objMemo != null && objMemo != DBNull.Value)
            {
                detail.CMEMO = objMemo.ToString();
            }
            frmServiceItemPlanAdd form = new frmServiceItemPlanAdd();
            form.Type = 2;
            form.Mode = ControlClassModeEnum.emEdit;
            form.CustomerServiceItemReservationTime = detail;
            if (form.ShowDialog() == DialogResult.OK)
            {
                detail = form.CustomerServiceItemReservationTime;
                dr[DBEntityCustomerServiceItemReservationTime.FieldName.CSEQNUM] = detail.CSEQNUM;
                dr[DBEntityCustomerServiceItemReservationTime.FieldName.CDAYS] = detail.CDAYS;
                dr[DBEntityCustomerServiceItemReservationTime.FieldName.CMEMO] = detail.CMEMO;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int iRowHandler = gvPlan.FocusedRowHandle;
            if (iRowHandler < 0)
            {
                MsgBox.MsgInfo("请选择要删除的记录。");
                return;
            }
            string sMsg = "确定要删除此记录吗？";
            if (MsgBox.MsgQue(sMsg))
            {
                DataRow dr = gvPlan.GetDataRow(iRowHandler);
                dr.Delete();
            }
        }

        private void frmCustomerServicePlan_Shown(object sender, EventArgs e)
        {
            InitCustomer();
            InitServiceItem();
            InitCustomerServiceItemDetail();
        }

        private void InitCustomer()
        {
            int iCustomerID = _customerserviceitem.CUSTOMERID;
            string error = string.Empty;
            DataSet dsCustomer = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerID(iCustomerID, out dsCustomer, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取会员信息失败\r\n" + error);
                return;
            }
            if (dsCustomer != null && dsCustomer.Tables.Count > 0 && dsCustomer.Tables[0].Rows.Count > 0)
            {
                DataRow drCustomer = dsCustomer.Tables[0].Rows[0];
                DBEntityCustomInfo customerInfo = new DBEntityCustomInfo(drCustomer);

                lblCustomerName.Text = customerInfo.CNAME;
                lblNickName.Text = customerInfo.CNICKNAME;
                lblPhone.Text = customerInfo.CMOBILEPHONE;
                lblLastVisited.Text = customerInfo.CLASTREGISTERDATE.ToString("yyyy-MM-dd");
            }
            
        }

        private void InitServiceItem()
        {
            lblItemName.Text = _customerserviceitem.SNAME;
            lblItemCode.Text = _customerserviceitem.SERVICEITEMCODE;
            lblPrice.Text = _customerserviceitem.SPRICE.ToString();
            lblTimeSpan.Text = _customerserviceitem.STIMESPAN.ToString();
        }

        private void InitCustomerServiceItemDetail()
        {
            string sFilter = string.Format("{0}={1}", DBEntityCustomerServiceItemReservationTime.FieldName.CUSTOMERSERVICEITEMID, _customerserviceitem.CUSTOMERSERVICEITEMID);
            string error = string.Empty;
            dsCustomerServiceItemDetail = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerServiceItemReservationTime(sFilter, out dsCustomerServiceItemDetail, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取会员服务项目明细信息失败\r\n" + error);
                return;
            }
            else
            {
                DataColumn dc = new DataColumn("ISSTANDARDPLAN");
                dc.DefaultValue = 0;
                dsCustomerServiceItemDetail.Tables[0].Columns.Add(dc);

                gdPlan.DataSource = null;
                gdPlan.DataSource = dsCustomerServiceItemDetail.Tables[0].DefaultView;
            }
        }
    }
}
